<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="850px"
      :clickModalClose="true"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
        <el-form-item label="案件类型" prop="caseType">
          <el-select class="flex-1" v-model="formData.caseType" placeholder="请选择案件类型">
            <el-option label="专利" value="1" />
            <el-option label="商标" value="2" />
            <el-option label="版权" value="3" />
            <el-option label="科技服务" value="4" />
            <el-option label="法律案件" value="5" />
            <el-option label="调查案" value="6" />
          </el-select>
        </el-form-item>
        <el-form-item label="要求类型" prop="requireType">
          <el-select class="flex-1" v-model="formData.requireType" placeholder="请选择要求类型">
            <el-option label="看稿要求" value="1" />
            <el-option label="费用要求" value="2" />
            <el-option label="其他要求" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="启用" prop="isValid">
          <el-radio-group v-model="formData.isValid">
            <el-radio label="0">是</el-radio>
            <el-radio label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="附件" prop="fileName">
          <el-button type="primary"> 选择文件 </el-button>
          <!-- <el-input v-model="formData.fileName" placeholder="请输入附件名称" /> -->
        </el-form-item>
        <el-form-item label="要求标题" prop="title">
          <el-input v-model="formData.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="要求内容" prop="description">
          <editor v-model="formData.description" height="500px" />
        </el-form-item>
        <el-form-item label="更新者" prop="updateUserName">
          <!-- <el-input v-model="formData.updateUserName" value="流程01" /> -->
          <p>流程01</p>
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {
  customerRequireEdit,
  customerRequireAdd,
  customerRequireDetail
} from '@/api/customer/customerRequire'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({})
  }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
  return mode.value == 'edit' ? '编辑客户要求' : '新增客户要求'
})

const formData = reactive({
  id: '',
  caseType: '',
  requireType: '',
  fileName: '',
  title: '',
  description: '',
  isValid: '0',
  updateUserName: ''
})

const formRules = {
  title: [
    {
      required: true,
      message: '请输入标题',
      trigger: ['blur']
    }
  ],
  description: [
    {
      required: true,
      message: '请输入描述',
      trigger: ['blur']
    }
  ]
}

const handleSubmit = async () => {
  await formRef.value?.validate()
  const data: any = { ...formData }
  mode.value == 'edit' ? await customerRequireEdit(data) : await customerRequireAdd(data)
  popupRef.value?.close()
  feedback.msgSuccess('操作成功')
  emit('success')
}

const open = (type = 'add') => {
  mode.value = type
  popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key]
    }
  }
}

const getDetail = async (row: Record<string, any>) => {
  const data = await customerRequireDetail({
    id: row.id
  })
  setFormData(data)
}

const handleClose = () => {
  emit('close')
}

defineExpose({
  open,
  setFormData,
  getDetail
})
</script>
